package com.example.myapplication.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.BaseFgm;
import com.example.myapplication.R;
import com.example.myapplication.activity.LoginAty;
import com.example.myapplication.activity.UserInfoAty;
import com.example.myapplication.database.User;
import com.example.myapplication.util.ActivityStack;
import com.example.myapplication.util.DbUtils;
import com.example.myapplication.util.GlideUtil;
import com.example.myapplication.util.SpUtil;
import com.example.myapplication.util.ToastUtil;

import org.litepal.LitePal;

public class MineFgm  extends BaseFgm {
    private TextView tvName;
    private ImageView userIcon;
    private View btWallet,btMyPub,btLoginOut,btUserInfo;

    @Override
    protected void logicStart() {
        User user = LitePal.where("phone ='" + SpUtil.getLoginPhone() + "'").findFirst(User.class);
        tvName.setText(user.getName());
        GlideUtil.loadHead(user.getAva(),userIcon);


    }

    @Override
    protected void initView() {
        tvName=rootView.findViewById(R.id.userNick);
        userIcon=rootView.findViewById(R.id.userIcon);
        btWallet=rootView.findViewById(R.id.btMyWallet);
        btMyPub=rootView.findViewById(R.id.btMypub);
        btLoginOut=rootView.findViewById(R.id.btLoginOut);
        btUserInfo=rootView.findViewById(R.id.btUserInfo);

    }

    @Override
    protected void setListener() {
        btUserInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ActivityStack.INSTANCE.startTo(getActivity(), UserInfoAty.class,null);
                User loginUser = DbUtils.getLoginUser();
                loginUser.update(loginUser.getMoney());
            }
        });
        btLoginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SpUtil.saveLoginStatus(false);
                ToastUtil.toast("退出登录成功");
                ActivityStack.INSTANCE.startTo(getActivity(), LoginAty.class,null);
                getActivity().finish();
            }
        });

    }

    @Override
    public void refreshState() {
        super.refreshState();
        logicStart();
    }

    @Override
    protected int getlayout() {
        return R.layout.fgm_mine;
    }
}
